/*
 * Copyright 2011 Philippe Blanc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package me.l1k3.app.fx.demo.client;

import me.l1k3.fx.client.FXChannel;
import me.l1k3.fx.client.FXLoops;
import me.l1k3.fx.client.channel.OpacityChannel;
import me.l1k3.fx.client.channel.BackgroundPositionChannel;
import me.l1k3.parameter.client.Callback;
import me.l1k3.parameter.client.Parameter;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Random;

public class Wallpaper implements EntryPoint {
    
    @Override
    public void onModuleLoad() {
        Parameter.load("wallpaper", new Callback() {
            @Override
            public void run(JavaScriptObject js) {
                demo(js);
            }
        });
    }
    
    protected void demo(JavaScriptObject js) {
        //Parameters parameters = js.cast();
        
        for(int i=0;i<12;i++) {
            animeBackgroundLayer("layer"+(i+1), 250*(i+1), (i%2==0));
        }
        
        for(int i=0;i<12;i++) {
            animeVBackgroundLayer("vlayer"+(i+1), 250*(i+1), (i%2==0));
        }
    }
    
    protected void animeBackgroundLayer(String name, int duration, boolean down) {
        Element element = DOM.getElementById(name);
      
        if(element==null) {
            return;
        }

        FXChannel layer = FXChannel.create(element);
        
        int position = (int)(Random.nextDouble()*400)+duration+600;
        
        if(!down) {
            position = -position;
        }
        
        layer.add(BackgroundPositionChannel.byIncrement(layer.getType(), 0, position));
        layer.add(OpacityChannel.range(layer.getType(), down? 0.75 : 0.1, down? 0.1 : 0.75));
        
        FXLoops loops = new FXLoops(layer, FXLoops.INFINITY, 4500, true);
        loops.animate(40000+duration, 1500+duration, false);
    }
    
    protected void animeVBackgroundLayer(String name, int duration, boolean left) {
        Element element = DOM.getElementById(name);
      
        if(element==null) {
            return;
        }
        
        FXChannel layer = FXChannel.create(element);
        
        int position = (int)(Random.nextDouble()*400)+duration+600;
        
        if(!left) {
            position = -position;
        }
        
        layer.add(BackgroundPositionChannel.byIncrement(layer.getType(), position, 0));

        layer.add(OpacityChannel.range(layer.getType(), left? 0.75 : 0.1, left? 0.1 : 0.75));
        
        FXLoops loops = new FXLoops(layer, FXLoops.INFINITY, 4500, true);
        loops.animate(40000+duration, 1500+duration, false);
    }
    
    protected static class Parameters extends JavaScriptObject {
        
        protected Parameters() {
            
        }
        
        public final native String get(String key, String defaultValue) /*-{
            return this[key] || defaultValue;
        }-*/;
        
        public final native boolean get(String key, boolean defaultValue) /*-{
            return this[key] || defaultValue;
        }-*/;
        
        public final native int get(String key, int defaultValue) /*-{
            return this[key] || defaultValue;
        }-*/;
        
        public final native double get(String key, double defaultValue) /*-{
            return this[key] || defaultValue;
        }-*/;
    }
}
